/*
 * Util.cpp
 *
 *  Created on: Apr 30, 2012
 *      Author: kobe
 */

#include "Util.h"

Proposition* createProposition(std::string input) {
	std::string type(input, 0, 2);

	if (type.compare("ap") == 0) {
		//atomic proposition
		return new AtomicProposition(input);
	} else if (type.compare("aa") == 0) {
		//atomic action
		return new AtomicAction(input);
	} else if (type.compare("tp") == 0) {
		//top
		return new Top(input);
	} else if (type.compare("bt") == 0) {
		//bottom
		return new Bottom(input);
	} else if (type.compare("cj") == 0) {
		//conjunction
		return new Conjunction(input);
	} else if (type.compare("dj") == 0) {
		//disjunction
		return new Disjunction(input);
	} else if (type.compare("bd") == 0) {
		//black diamond
		return new BlackDiamond(input);
	} else if (type.compare("sq") == 0) {
		//square
		return new Square(input);
	} else if (type.compare("md") == 0) {
		//monoid
		return new Monoid(input);
	} else if (type.compare("af") == 0) {
		//after
		return new After(input);
	} else if (type.compare("up") == 0) {
		//update
		return new Update(input);
	} else {
		std::cout << "Parsing failed:" << type << std::endl;
		return NULL;
	}
}

std::string skipBracket(std::string input) {
	size_t first = input.find('{');
	std::string result;
	if (first == std::string::npos) {
		return result;
	} else {
		int i, count = 0;
		for (i = first; i < input.size(); i++) {
			if (input.at(i) == '{')
				count++;
			if (input.at(i) == '}')
				count--;
			if (count == 0)
				break;
		}
		if (input.size() - i <= 1) {
			return result;
		} else {
			std::string t(input, i + 1, input.size() - i);
			result = t;
			return result;
		}
		return result;
	}
}

std::string getBracketContent(std::string input, size_t* start, size_t* end) {
	size_t first = input.find('{');
	if (first == std::string::npos) {
		return 0;
	} else {
		*start = first + 1;
		int i, count = 0;
		for (i = first; i < input.size(); i++) {
			if (input.at(i) == '{')
				count++;
			if (input.at(i) == '}')
				count--;
			if (count == 0)
				break;
		}
		*end = i - 1;
		std::string result(input, *start, *end - *start + 1);
		return result;
	}
}


